package com.work.libtest2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.dto.ExtraWorkDto;
import com.work.libtest2.entity.*;
import com.work.libtest2.mapper.*;
import com.work.libtest2.service.ExtraWorkService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * ClassName: ExtraWorkServiceImpl
 * Package: com.work.libtest2.service.impl
 * Description:
 *
 * @Author
 * @Create 2024/6/10 15:33
 * @Version 1.0
 */
@Service
public class ExtraWorkServiceImpl extends ServiceImpl<ExtraWorkMapper, ExtraWork> implements ExtraWorkService {

    @Resource
    private ExtraWorkMapper extraWorkMapper;
    @Resource
    private ExtraWorkTypeMapper extraWorkTypeMapper;
    @Resource
    private DepartmentMapper deptMapper;
    @Resource
    private JobMapper jobMapper;
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public IPage<ExtraWorkDto> getPage(Integer currentPage, Integer pageSize, ExtraWorkDto extraWorkDto) {
        LambdaQueryWrapper<ExtraWork> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(extraWorkDto.getEmpName()),ExtraWork::getEmpName,extraWorkDto.getEmpName());
        IPage page = new Page(currentPage,pageSize);
        extraWorkMapper.selectPage(page,lqw);

        List<ExtraWorkDto> list = new ArrayList<>();
        IPage newPage = new Page();
        BeanUtils.copyProperties(page,newPage);
        newPage.setRecords(list);

        for (int i = 0; i < page.getRecords().size(); i++) {
            ExtraWorkDto extraWork = new ExtraWorkDto();
            BeanUtils.copyProperties(page.getRecords().get(i),extraWork);
//            获取加班类型以及报酬
            ExtraWorkType extraWorkType = extraWorkTypeMapper.selectById(extraWork.getEwType());
            extraWork.setEwTypeName(extraWorkType.getEwTypeName());
            extraWork.setEwPay("￥ "+extraWorkType.getEwPay());
//           获取部门名和工种名
            Employee employee = employeeMapper.selectById(extraWork.getEmpId());
            Department department = deptMapper.selectById(employee.getDeptId());
            extraWork.setDeptName(department.getDeptName());

            Job job = jobMapper.selectById(employee.getJobId());
            extraWork.setJobName(job.getJobName());
            list.add(extraWork);
        }
//        对日期范围区间进行判断
        if (extraWorkDto.getStartDate()!=""&&extraWorkDto.getStartDate()!=null&& !Objects.equals(String.valueOf(extraWorkDto.getStartDate()), "null")){
            List<ExtraWorkDto> extraWorkDtos = new ArrayList<>();
            try {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = dateFormat.parse(extraWorkDto.getStartDate());
                    for (int i = 0; i < list.size(); i++) {
                        String ewDate = String.valueOf(list.get(i).getEwDate());
                        Date ewDateStr = dateFormat.parse(ewDate);
                        if (date.before(ewDateStr)){
                            extraWorkDtos.add(list.get(i));
                        }
                    }
                    list = extraWorkDtos;
                    newPage.setRecords(list);
                    newPage.setTotal(list.size());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }

        if (extraWorkDto.getEndDate()!=null&&extraWorkDto.getEndDate()!=""&& !Objects.equals(extraWorkDto.getEndDate(), "null")){
            List<ExtraWorkDto> extraWorkDtos = new ArrayList<>();
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateFormat.parse(extraWorkDto.getEndDate());

                for (int i = 0; i < list.size(); i++) {
                    String ewDate = String.valueOf(list.get(i).getEwDate());
                    Date ewDateStr = dateFormat.parse(ewDate);
                    if (date.after(ewDateStr)){
                        extraWorkDtos.add(list.get(i));
                    }
                }
                list = extraWorkDtos;
                newPage.setRecords(list);
                newPage.setTotal(list.size());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return newPage;
    }

    @Override
    public List<ExtraWork> getAllEmpName() {
        return extraWorkMapper.getAllEmpName();
    }

    @Override
    public int addExtraWork(ExtraWork extraWork) {
        Employee employee = employeeMapper.selectById(extraWork.getEmpId());
        extraWork.setEmpName(employee.getEmpName());
        return extraWorkMapper.insert(extraWork);
    }

    @Override
    public ExtraWork getById(Integer ewId) {
        return extraWorkMapper.selectById(ewId);
    }

    @Override
    public int updateExtraWork(ExtraWork extraWork) {
        return extraWorkMapper.updateById(extraWork);
    }

    @Override
    public int deleteById(Integer ewId) {
        return extraWorkMapper.deleteById(ewId);
    }
}
